package com.ssy.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.ssy.common.CodeMsg;
import com.ssy.common.PageInfo;
import com.ssy.common.Result;
import com.ssy.dao.CourseDao;
import com.ssy.entity.Course;
import com.ssy.service.CourseService;

import java.util.HashMap;

/**
 * CourseServiceImpl
 *
 * @author ssy
 * @date 2020/11/20
 **/
public class CourseServiceImpl implements CourseService {

    CourseDao courseDao = new CourseDao();

    @Override
    public Result updateCourse(String courseId, String number, String name, String classHours, String credit) {
        int courseIdNo = Integer.parseInt(courseId);
        int numberNo = Integer.parseInt(number);
        int classHoursNo = Integer.parseInt(classHours);
        int creditNo = Integer.parseInt(credit);

        Course course = this.courseDao.selectCourseById(courseIdNo);

        if (course.getNumber() != numberNo) {
            Course course2 = this.courseDao.selectCourseByNumber(numberNo);
            if (course2 != null) {
                return new Result(CodeMsg.COURSE_NUMBER_ALREADY_EXISTS);
            }
        }

        if (!course.getName().equals(name)) {
            Course course2 = this.courseDao.selectCourseByName(name);
            if (course2 != null) {
                return new Result(CodeMsg.COURSE_NAME_ALREADY_EXISTS);
            }
        }

        boolean flag = this.courseDao.modifyCourse(numberNo, name, classHoursNo, creditNo, courseIdNo);
        return flag ? new Result(CodeMsg.UPDATE_SUCCES) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public Result deleteOneCourse(String id) {
        int idNo = Integer.parseInt(id);
        boolean flag = this.courseDao.deleteCourse(idNo);
        return flag ? new Result(CodeMsg.DELETE_SUCCES) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result BathdeleteCourse(String[] ids) {
        boolean flag = this.courseDao.batchDeleteCourse(ids);
        return flag ? new Result(CodeMsg.DELETE_SUCCES) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result addCourse(String number, String name, String classHours, String credit) {
        int numberNo = Integer.parseInt(number);
        int classHoursNo = Integer.parseInt(classHours);
        int creditNo = Integer.parseInt(credit);

        Course course = this.courseDao.selectCourseByNumber(numberNo);
        if (course != null) {
            return new Result(CodeMsg.COURSE_NUMBER_ALREADY_EXISTS);
        }

        Course course2 = this.courseDao.selectCourseByName(name);
        if (course2 != null) {
            return new Result(CodeMsg.COURSE_NAME_ALREADY_EXISTS);
        }

        boolean flag = this.courseDao.insertCourse(numberNo, name, classHoursNo, creditNo);
        return flag ? new Result(CodeMsg.ADD_SUCCES) : new Result(CodeMsg.ADD_ERROR);
    }

    @Override
    public Result queryPage(HashMap<String, Object> param, String page, String limit) {
        Integer pageNo = NumberUtil.isNumber(page) ? Integer.parseInt(page) : 1;
        Integer limitNo = NumberUtil.isNumber(limit) ? Integer.parseInt(limit) : 10;
        PageInfo<Course> classPageInfo = this.courseDao.selectPage(param, pageNo, limitNo);
        return new Result(classPageInfo);
    }
}
